package com.example.pcdemo.view;

import android.content.Context;
import android.content.pm.Attribution;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.pcdemo.R;

/**
 * 功能：
 * 作者：HeXiangGuang
 * 日期：2024年05月22日
 */
public class XImageView extends FrameLayout {
    private static final String TAG = "XImageView";
    private Context context;
    private ImageView imageView;


    public XImageView(@NonNull Context context) {
        this(context,null);
    }

    public XImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public XImageView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        View view = LayoutInflater.from(context).inflate(R.layout.x_imgview, null);
        this.imageView=view.findViewById(R.id.img_view);
        this.addView(view);
    }
    public void setImgSrc(int imgSrc){
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),imgSrc);
        int height = bitmap.getHeight();
        int width = bitmap.getWidth();
        Log.e(TAG, "setImgSrc: "+height+"   "+width );
        Bitmap bmp = Bitmap.createBitmap(bitmap,0,0,height-141,width-121,null,false);
        imageView.setImageBitmap(bmp);
        if(bitmap !=null&& !bitmap.equals(bmp) && !bitmap.isRecycled()) {
            bitmap.recycle();//回收原图片
        }
    }
}
